package edu.hcmiu.portal.service.persistence;

import com.liferay.portal.SystemException;
import com.liferay.portal.kernel.annotation.BeanReference;
import com.liferay.portal.kernel.cache.CacheRegistry;
import com.liferay.portal.kernel.dao.orm.DynamicQuery;
import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderPath;
import com.liferay.portal.kernel.dao.orm.Query;
import com.liferay.portal.kernel.dao.orm.QueryUtil;
import com.liferay.portal.kernel.dao.orm.Session;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.model.ModelListener;
import com.liferay.portal.service.persistence.BatchSessionUtil;
import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;

import edu.hcmiu.portal.NoSuchhook_activityException;
import edu.hcmiu.portal.model.hook_activity;
import edu.hcmiu.portal.model.impl.hook_activityImpl;
import edu.hcmiu.portal.model.impl.hook_activityModelImpl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


public class hook_activityPersistenceImpl extends BasePersistenceImpl
    implements hook_activityPersistence {
    public static final String FINDER_CLASS_NAME_ENTITY = hook_activityImpl.class.getName();
    public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
        ".List";
    public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(hook_activityModelImpl.ENTITY_CACHE_ENABLED,
            hook_activityModelImpl.FINDER_CACHE_ENABLED,
            FINDER_CLASS_NAME_LIST, "findAll", new String[0]);
    public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(hook_activityModelImpl.ENTITY_CACHE_ENABLED,
            hook_activityModelImpl.FINDER_CACHE_ENABLED,
            FINDER_CLASS_NAME_LIST, "countAll", new String[0]);
    private static Log _log = LogFactoryUtil.getLog(hook_activityPersistenceImpl.class);
    @BeanReference(name = "edu.hcmiu.portal.service.persistence.hook_activityPersistence.impl")
    protected edu.hcmiu.portal.service.persistence.hook_activityPersistence hook_activityPersistence;
    @BeanReference(name = "edu.hcmiu.portal.service.persistence.hook_categoryPersistence.impl")
    protected edu.hcmiu.portal.service.persistence.hook_categoryPersistence hook_categoryPersistence;
    @BeanReference(name = "edu.hcmiu.portal.service.persistence.hook_taglistPersistence.impl")
    protected edu.hcmiu.portal.service.persistence.hook_taglistPersistence hook_taglistPersistence;
    @BeanReference(name = "edu.hcmiu.portal.service.persistence.hook_userPersistence.impl")
    protected edu.hcmiu.portal.service.persistence.hook_userPersistence hook_userPersistence;

    public void cacheResult(hook_activity hook_activity) {
        EntityCacheUtil.putResult(hook_activityModelImpl.ENTITY_CACHE_ENABLED,
            hook_activityImpl.class, hook_activity.getPrimaryKey(),
            hook_activity);
    }

    public void cacheResult(List<hook_activity> hook_activities) {
        for (hook_activity hook_activity : hook_activities) {
            if (EntityCacheUtil.getResult(
                        hook_activityModelImpl.ENTITY_CACHE_ENABLED,
                        hook_activityImpl.class, hook_activity.getPrimaryKey(),
                        this) == null) {
                cacheResult(hook_activity);
            }
        }
    }

    public void clearCache() {
        CacheRegistry.clear(hook_activityImpl.class.getName());
        EntityCacheUtil.clearCache(hook_activityImpl.class.getName());
        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
    }

    public hook_activity create(long actID) {
        hook_activity hook_activity = new hook_activityImpl();

        hook_activity.setNew(true);
        hook_activity.setPrimaryKey(actID);

        return hook_activity;
    }

    public hook_activity remove(long actID)
        throws NoSuchhook_activityException, SystemException {
        Session session = null;

        try {
            session = openSession();

            hook_activity hook_activity = (hook_activity) session.get(hook_activityImpl.class,
                    new Long(actID));

            if (hook_activity == null) {
                if (_log.isWarnEnabled()) {
                    _log.warn("No hook_activity exists with the primary key " +
                        actID);
                }

                throw new NoSuchhook_activityException(
                    "No hook_activity exists with the primary key " + actID);
            }

            return remove(hook_activity);
        } catch (NoSuchhook_activityException nsee) {
            throw nsee;
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }
    }

    public hook_activity remove(hook_activity hook_activity)
        throws SystemException {
        for (ModelListener<hook_activity> listener : listeners) {
            listener.onBeforeRemove(hook_activity);
        }

        hook_activity = removeImpl(hook_activity);

        for (ModelListener<hook_activity> listener : listeners) {
            listener.onAfterRemove(hook_activity);
        }

        return hook_activity;
    }

    protected hook_activity removeImpl(hook_activity hook_activity)
        throws SystemException {
        Session session = null;

        try {
            session = openSession();

            if (hook_activity.isCachedModel() || BatchSessionUtil.isEnabled()) {
                Object staleObject = session.get(hook_activityImpl.class,
                        hook_activity.getPrimaryKeyObj());

                if (staleObject != null) {
                    session.evict(staleObject);
                }
            }

            session.delete(hook_activity);

            session.flush();
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }

        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);

        EntityCacheUtil.removeResult(hook_activityModelImpl.ENTITY_CACHE_ENABLED,
            hook_activityImpl.class, hook_activity.getPrimaryKey());

        return hook_activity;
    }

    /**
     * @deprecated Use <code>update(hook_activity hook_activity, boolean merge)</code>.
     */
    public hook_activity update(hook_activity hook_activity)
        throws SystemException {
        if (_log.isWarnEnabled()) {
            _log.warn(
                "Using the deprecated update(hook_activity hook_activity) method. Use update(hook_activity hook_activity, boolean merge) instead.");
        }

        return update(hook_activity, false);
    }

    /**
     * Add, update, or merge, the entity. This method also calls the model
     * listeners to trigger the proper events associated with adding, deleting,
     * or updating an entity.
     *
     * @param                hook_activity the entity to add, update, or merge
     * @param                merge boolean value for whether to merge the entity. The
     *                                default value is false. Setting merge to true is more
     *                                expensive and should only be true when hook_activity is
     *                                transient. See LEP-5473 for a detailed discussion of this
     *                                method.
     * @return                true if the portlet can be displayed via Ajax
     */
    public hook_activity update(hook_activity hook_activity, boolean merge)
        throws SystemException {
        boolean isNew = hook_activity.isNew();

        for (ModelListener<hook_activity> listener : listeners) {
            if (isNew) {
                listener.onBeforeCreate(hook_activity);
            } else {
                listener.onBeforeUpdate(hook_activity);
            }
        }

        hook_activity = updateImpl(hook_activity, merge);

        for (ModelListener<hook_activity> listener : listeners) {
            if (isNew) {
                listener.onAfterCreate(hook_activity);
            } else {
                listener.onAfterUpdate(hook_activity);
            }
        }

        return hook_activity;
    }

    public hook_activity updateImpl(
        edu.hcmiu.portal.model.hook_activity hook_activity, boolean merge)
        throws SystemException {
        Session session = null;

        try {
            session = openSession();

            BatchSessionUtil.update(session, hook_activity, merge);

            hook_activity.setNew(false);
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }

        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);

        EntityCacheUtil.putResult(hook_activityModelImpl.ENTITY_CACHE_ENABLED,
            hook_activityImpl.class, hook_activity.getPrimaryKey(),
            hook_activity);

        return hook_activity;
    }

    public hook_activity findByPrimaryKey(long actID)
        throws NoSuchhook_activityException, SystemException {
        hook_activity hook_activity = fetchByPrimaryKey(actID);

        if (hook_activity == null) {
            if (_log.isWarnEnabled()) {
                _log.warn("No hook_activity exists with the primary key " +
                    actID);
            }

            throw new NoSuchhook_activityException(
                "No hook_activity exists with the primary key " + actID);
        }

        return hook_activity;
    }

    public hook_activity fetchByPrimaryKey(long actID)
        throws SystemException {
        hook_activity hook_activity = (hook_activity) EntityCacheUtil.getResult(hook_activityModelImpl.ENTITY_CACHE_ENABLED,
                hook_activityImpl.class, actID, this);

        if (hook_activity == null) {
            Session session = null;

            try {
                session = openSession();

                hook_activity = (hook_activity) session.get(hook_activityImpl.class,
                        new Long(actID));
            } catch (Exception e) {
                throw processException(e);
            } finally {
                if (hook_activity != null) {
                    cacheResult(hook_activity);
                }

                closeSession(session);
            }
        }

        return hook_activity;
    }

    public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
        throws SystemException {
        Session session = null;

        try {
            session = openSession();

            dynamicQuery.compile(session);

            return dynamicQuery.list();
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }
    }

    public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
        int start, int end) throws SystemException {
        Session session = null;

        try {
            session = openSession();

            dynamicQuery.setLimit(start, end);

            dynamicQuery.compile(session);

            return dynamicQuery.list();
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }
    }

    public List<hook_activity> findAll() throws SystemException {
        return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
    }

    public List<hook_activity> findAll(int start, int end)
        throws SystemException {
        return findAll(start, end, null);
    }

    public List<hook_activity> findAll(int start, int end, OrderByComparator obc)
        throws SystemException {
        Object[] finderArgs = new Object[] {
                String.valueOf(start), String.valueOf(end), String.valueOf(obc)
            };

        List<hook_activity> list = (List<hook_activity>) FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
                finderArgs, this);

        if (list == null) {
            Session session = null;

            try {
                session = openSession();

                StringBuilder query = new StringBuilder();

                query.append("FROM edu.hcmiu.portal.model.hook_activity ");

                if (obc != null) {
                    query.append("ORDER BY ");
                    query.append(obc.getOrderBy());
                }

                Query q = session.createQuery(query.toString());

                if (obc == null) {
                    list = (List<hook_activity>) QueryUtil.list(q,
                            getDialect(), start, end, false);

                    Collections.sort(list);
                } else {
                    list = (List<hook_activity>) QueryUtil.list(q,
                            getDialect(), start, end);
                }
            } catch (Exception e) {
                throw processException(e);
            } finally {
                if (list == null) {
                    list = new ArrayList<hook_activity>();
                }

                cacheResult(list);

                FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);

                closeSession(session);
            }
        }

        return list;
    }

    public void removeAll() throws SystemException {
        for (hook_activity hook_activity : findAll()) {
            remove(hook_activity);
        }
    }

    public int countAll() throws SystemException {
        Object[] finderArgs = new Object[0];

        Long count = (Long) FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
                finderArgs, this);

        if (count == null) {
            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(
                        "SELECT COUNT(*) FROM edu.hcmiu.portal.model.hook_activity");

                count = (Long) q.uniqueResult();
            } catch (Exception e) {
                throw processException(e);
            } finally {
                if (count == null) {
                    count = Long.valueOf(0);
                }

                FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
                    count);

                closeSession(session);
            }
        }

        return count.intValue();
    }

    public void afterPropertiesSet() {
        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
                    com.liferay.util.service.ServiceProps.get(
                        "value.object.listener.edu.hcmiu.portal.model.hook_activity")));

        if (listenerClassNames.length > 0) {
            try {
                List<ModelListener<hook_activity>> listenersList = new ArrayList<ModelListener<hook_activity>>();

                for (String listenerClassName : listenerClassNames) {
                    listenersList.add((ModelListener<hook_activity>) Class.forName(
                            listenerClassName).newInstance());
                }

                listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
            } catch (Exception e) {
                _log.error(e);
            }
        }
    }
}
